//
//  LBMeShippingAddrController.m
//  loveBeen
//
//  Created by lc on 2016/11/23.
//  Copyright © 2016年 loveBeen. All rights reserved.
//

#import "LBMeShippingAddrController.h"
#import "LBMeAddrCell.h"
#import "LBMeAddrModel.h"
#import "LBMeUpdateAddrController.h"
#import "LBMeUpdateAddrModel.h"

static NSString *ID = @"cell_id";

@interface LBMeShippingAddrController ()<UITableViewDelegate,UITableViewDataSource,LBMeAddrCellDelegate,LBMeUpdateAddrControllerDelegate>
@property(nonatomic,weak)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray<LBMeAddrModel *> *listModel;
@property(nonatomic,strong)NSIndexPath *indexPath;
@end


@implementation LBMeShippingAddrController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self loadData];
    [self setupUI];
}

//跳转到更改或删除联系人信息
-(void)updateAddr:(LBMeAddrCell *)cell {
    //获取indexPath属性
  //  NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
//    NSLog(@"======%@=====",indexPath);
    
    //保存当前的的indexpath以便于删除
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    self.indexPath = indexPath;
    //添加一个model
    LBMeUpdateAddrModel *model = [[LBMeUpdateAddrModel alloc] init];
    model.contactName = cell.lb_userName.text;
    model.contactPhoneNum = cell.lb_userPhone.text;
    model.contactAddr = cell.lb_userAddress.text;
    
    LBMeUpdateAddrController *updateAddrVc = [[LBMeUpdateAddrController alloc] initWithStyle:UITableViewStyleGrouped];
    
    //设置代理
    updateAddrVc.delegate = self;
    
    //赋值到控制器model属性实现数据传递
    updateAddrVc.model = model;
  //  updateAddrVc.indexPath = indexPath;
    [self.navigationController pushViewController:updateAddrVc animated:YES];
}

-(void)viewWillAppear:(BOOL)animated {
    //屏蔽统一navigationBar的样式，自定义样式
    [self.navigationController.navigationBar setTintColor:[UIColor grayColor]];
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
}

//删除当前行
-(void)deleteCurrentRow {
//    NSIndexPath *indext1 = [NSIndexPath indexPathForRow:0 inSection:0];
//    NSIndexPath *indext2 = [NSIndexPath indexPathForRow:1 inSection:0];
//    [self.tableView deleteRowsAtIndexPaths:@[_indexPath] withRowAnimation:UITableViewRowAnimationLeft];
//    [self.listModel removeObjectAtIndex:(NSUInteger)_indexPath.row];
//    [self.tableView reloadData];
//    
    
    
}

-(void)loadData {
    
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"LBMeAddrCell.plist" withExtension:nil];
    NSArray *array = [NSArray arrayWithContentsOfURL:url];
    NSArray *tempArray = [NSArray yy_modelArrayWithClass:[LBMeAddrModel class] json:array];
    self.listModel = tempArray.copy;
    [self.tableView reloadData];
}

- (void)setupUI {
    
    //设置title
    self.navigationItem.title = @"我的收获地址";
    
    //创建一个tableView
    UITableView *tableView = [[UITableView alloc] init];
    [self.view addSubview:tableView];
//    tableView.backgroundColor = [UIColor grayC];
    
    tableView.backgroundColor = [UIColor colorWithRed:228/255.0 green:228/255.0 blue:228/255.0 alpha:1];
    //添加一个footerView
    UIView *footerView = [[UIView alloc] init];
    tableView.tableFooterView = footerView;
    
    //创建一个view添加增加按钮
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitle:@"+新增地址" forState:UIControlStateNormal];
    button.font = [UIFont systemFontOfSize:14];
    [button setBackgroundImage:[UIImage imageNamed:@"v2_coupon_verify_normal"] forState:UIControlStateNormal];
    [view addSubview:button];
    
    //添加到selfview上
    [self.view addSubview:view];
    
    //设置代理
    tableView.dataSource = self;
    tableView.delegate = self;
    
    //约束
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(view);
        make.size.mas_equalTo(CGSizeMake(300, 28));
    }];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    
    //关联
    self.tableView = tableView;
    
    //自动预估高度
//    tableView.estimatedRowHeight = 40;
//    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.rowHeight = 80;
    
     footerView.backgroundColor = [UIColor redColor];
    //注册cell
    //[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    [self.tableView registerNib:[UINib nibWithNibName:@"LBMeAddrCell" bundle:nil] forCellReuseIdentifier:ID];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listModel.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LBMeAddrCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.delegate = self;
    cell.model = self.listModel[indexPath.row];
    return cell;
}

//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}
//
//
//
//-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        [self.listModel removeObjectAtIndex:indexPath.row ];
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
//    }
//}


@end
